package cn.wanda.bi.sunshine.platform.server.controller;

import java.util.HashMap;
import java.util.List;

import cn.wanda.bi.sunshine.platform.server.aspect.CheckLimit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.wanda.bi.sunshine.platform.server.aspect.AccessLabel;
import cn.wanda.bi.sunshine.platform.server.bean.model.DimColumnLabelValue;
import cn.wanda.bi.sunshine.platform.server.controller.request.DimLabelAllRequest;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import cn.wanda.bi.sunshine.platform.server.service.LabelDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by James on 17/7/21.
 */
@Api(description = "标签字典")
@RestController
@RequestMapping(value = "/v1/label/dictionary", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DMPLabelDictionaryController {

    public static final Logger logger = LoggerFactory.getLogger(DMPLabelDictionaryController.class);

    @Autowired
    private LabelDictionaryService labelDictionaryService;

    @AccessLabel
    @ApiOperation("查询基础标签的version值，以列表的形式返回当前所有的version值")
    @RequestMapping(value = "/version/values", method = RequestMethod.GET)
    public SimpleResponse versionValues(
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return labelDictionaryService.queryVersionValues();
    }

    @CheckLimit
    @AccessLabel
    @ApiOperation("查询用户基础属性标签，输入是用户基础标签查询条件的组合，查询页码index和每页数据条数limit，输出结果是所支持的所有基础用户标签")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ListResponse userLabelAll(
            @ApiParam(value = "请求体") @RequestBody DimLabelAllRequest request,
            @ApiParam(value = "页码") @RequestParam(value = "index", required = false, defaultValue = "1") Integer index,
            @ApiParam(value = "每页个数") @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return labelDictionaryService.queryUserLabelAll(limit, index, request);
    }

    @AccessLabel
    @ApiOperation("查询用户基础属性标签值，输入是需要查询的标签属性，输出返回对应的标签所有值列表")
    @RequestMapping(value = "/query/values", method = RequestMethod.POST)
    public SimpleResponse userLabelValues(@ApiParam(value = "labels") @RequestBody List<String> labels,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
    	HashMap<String, Object> hashMap = new HashMap<>();
    	labels.forEach(p -> {
            List<DimColumnLabelValue> userLabelAllSupportedFieldsValueList = labelDictionaryService.getDimColumnLabelValues(p);
            hashMap.put(p, userLabelAllSupportedFieldsValueList);
        });
        return SimpleResponse.success(hashMap);
    }


}
